/**
 *  Copyright (C) 2010:
 *                 Buzzanca Marco <marco.bzn@gmail.com>
 *         Monaco Davide Giuseppe <black.ralkass@gmail.com>
 *              Petralia Veronica <veronicapetralia@gmail.com>
 *
 * This file is part of DS2011BMP
 *
 * DS2011BMP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DS2011BMP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * @file tqueuemanager.h
 * @author Davide Giuseppe Monaco
 *
 * This header file contains TQueueManager class declaration.
 */

#ifndef _T_QUEUE_MANAGER_H_
#define _T_QUEUE_MANAGER_H_ 1

#include <boost/bind.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <iostream>
#include <sstream>

#include "address.h"
#include "synque.h"
#include "udpinterface.h"
#include "udpserver.h"
#include "taskdescriptor.h"

#define MAX_BUF_SIZE 60000

/**
 * @brief TQueueManager manages the TaskDescriptor queue for the Worker module and
 * handles communication with Manager module.
 *
 * In order to receive enqueue() requests, TQueueManager creates an UDPServer.
 */
class TQueueManager
{
private:
  /**
   * @brief Worker Address.
   */
  Address _addr;

  /**
   * @brief TQueueManager queue. It's a synchronized container.
   */
  synque<TaskDescriptor> _tQueue;

  /**
   * @brief The UDP server that TQueueManager will use in order to
   *        receive enqueue() requests.
   */
  UDPServer _server;

  /**
   * @brief TQueueManager main thread callback member function.
   *
   * Listen for enqueue() requests and create a new worker thread
   * for its handling.
   */
  void run(void);

  /**
   * @brief TQueueManager worker thread callback ember function.
   *
   * Deserialize buf obtaining a TaskDescriptor end push() it in the queue.
   *
   * @param buf   A pointer to received buffer containing a
   *              serialized TaskDescriptor.
   */
  void serve(char *buf);

  /**
   * Main thread.
   */
  boost::shared_ptr<boost::thread> _s_thread;

public:
  /**
   * @brief Default Constructor.
   *
   * NOTE: This should not be used.
   */
  TQueueManager(void);

  /**
   * @brief Constructor specifying server Address.
   * @param addr  Server Address.
   */
  TQueueManager(const Address& addr);

  /**
   * @brief Destructor.
   */
  ~TQueueManager(void);

  /**
   * @brief XXX: Returns the current Worker Address.
   * @return  A constant reference to private member _addr.
   */
  const Address& getAddr(void) const;

  /**
   * @brief Sets current Worker Address to the provided Address.
   * @param val   The provided Address.
   */
  void setAddr(const Address& val);

  /**
   * @brief Push the provided TaskDescriptor into the queue.
   * @param   ts  The provided TaskDescriptor.
   * @return      Zero if operation successful, otherwise an error code.
   */
  int enqueue(const TaskDescriptor& ts);

  /**
   * @brief Pop from the queue the next TaskDescriptor and returns it.
   * @return      The next TaskDescriptor.
   */
  TaskDescriptor dequeue(void);

  /**
   * @brief Create the main thread and join on it.
   */
  void start(void);
};

#endif /* _T_QUEUE_MANAGER_H_ */
